package com.platform.manager.action.system;

import com.platform.common.mybatis.Page;
import com.platform.common.mybatis.context.ContextHolder;
import com.platform.common.util.JsonUtil;
import com.platform.domain.model.Department;
import com.platform.domain.result.DataGirdData;
import com.platform.domain.result.ResultData;
import com.platform.manager.action.BaseAction;
import com.platform.service.DepartmentService;
import org.apache.struts2.convention.annotation.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-10-09
 * Time: 下午2:34
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/system")
@Service("deptAction")
@ParentPackage("jsonPackage")
public class DeptAction extends BaseAction {

    @Resource
    private DepartmentService departmentService;

    /**
     * 查询功能信息列表
     *
     * @return
     */
    @Action(value = "queryDepts", interceptorRefs = @InterceptorRef("pageStack"),
            results = {@Result(name = "success", type = "json", params = {"root", "dataGirdData"})})
    public String queryDepts() {
        try {
            // 查询符合条件的用户列表
            List<Department> list = departmentService.select(null);
            dataGirdData = new DataGirdData();
            dataGirdData.setRows(list);
            // 获取翻页参数
            Page page = ContextHolder.getPage();
            dataGirdData.setTotal(page.getTotalResult());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 保存功能信息列表
     *
     * @return
     */
    @Action(value = "saveDepts", results = {@Result(name = "success", type = "json", params = {"root", "result"})})
    public String saveDepts() {
        try {
            if (jsonStr != null) {
                print("前台发送的JSON数据：" + jsonStr);
                Map<String, List> maps = JsonUtil.jsonToMap(jsonStr, Department.class);
                List inserted = maps.get("inserted");
                List updated = maps.get("updated");
                List deleted = maps.get("deleted");

                // 保存未插入、更新、删除成功的的用户登录名
                StringBuffer buffer = new StringBuffer();

                if (inserted != null) {
                    for(Object obj : inserted){
                        Department dept = (Department) obj;
                        int resInt = departmentService.insert(dept);
                        if (resInt != 1) {
                            buffer.append(" [").append(dept.getDeptId()).append("]");
                        }
                    }
                }

                if (updated != null) {
                    for(Object obj : updated){
                        Department dept = (Department) obj;
                        int resInt = departmentService.update(dept);
                        if (resInt != 1) {
                            buffer.append(" [").append(dept.getDeptId()).append("]");
                        }
                    }
                }

                if (deleted != null) {
                    for(Object obj : deleted){
                        Department dept = (Department) obj;
                        int resInt = departmentService.delete(dept);
                        if (resInt != 1) {
                            buffer.append(" [").append(dept.getDeptId()).append("]");
                        }
                    }
                }


                result = new ResultData();
                if (buffer.length() == 0) {
                    result.setSuccess(true);
                    result.setMessage("操作成功！");
                } else {
                    result.setSuccess(false);
                    result.setMessage("操作失败！\n以下部门操作失败！\n[部门ID]：" + buffer.toString());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }
}
